/**
 * MemberPersistenceServiceImpl.java 下午6:09:29 2012-2-28
 * Cyber Information Technology Co.,Ltd.
 *   
 *   http://www.citchina.cn
 *   citchina.org@gmail.com
 */
package cn.cit.wcif.member.persistence.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import cn.cit.wcif.member.api.IMember;
import cn.cit.wcif.member.api.exceptions.EmailExistenceException;
import cn.cit.wcif.member.api.exceptions.NameExistenceException;
import cn.cit.wcif.member.api.exceptions.ObjectNonExistenceException;
import cn.cit.wcif.member.api.persistence.IMemberEntity;
import cn.cit.wcif.member.api.persistence.IMemberPersistenceService;
import cn.cit.wcif.member.persistence.jpa.entity.MemberEntityImpl;

/**
 * @author Jonathan
 * @version 1.0
 * 
 */
@Repository
public class MemberPersistenceServiceImpl implements IMemberPersistenceService {
	@PersistenceContext
	private EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.cit.outman.member.api.persistence.MemberPersistenceService#persistMember
	 * (cn.cit.outman.member.api.Member)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public IMemberEntity persistMember(IMember member) {
		IMemberEntity entity = new MemberEntityImpl();
		entity.setId(member.getId());
		entity.setName(member.getName());
		em.persist(entity);

		List<IMemberEntity> result = em.createQuery(
				"select m from MemberEntityImpl m").getResultList();
		if (result != null && result.size() > 0) {
			return result.get(result.size() - 1);
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.cit.outman.member.api.persistence.MemberPersistenceService#
	 * findMemberByIdentity(java.lang.String, java.lang.String)
	 */
	@Override
	public IMemberEntity findMemberByIdentity(String email, String password)
			throws ObjectNonExistenceException {
		MemberEntityImpl result = null;
		MemberEntityImpl entity = new MemberEntityImpl();
		entity.setEmail(email);
		entity.setPassword(password);
		try {
			result = (MemberEntityImpl) em
					.createNamedQuery("Member.findByEmailAndPassword")
					.setParameter("email", email)
					.setParameter("password", password).getSingleResult();
		} catch (NoResultException e) {
			System.out.println("The Member(eMail:" + email + ") is not find!");
			throw new ObjectNonExistenceException("The Member(eMail:" + email
					+ ") is not find!");
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.cit.outman.member.api.persistence.MemberPersistenceService#saveMember
	 * (java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	public IMemberEntity saveMember(String email, String password, String name)
			throws EmailExistenceException, NameExistenceException {
		MemberEntityImpl entity = null;
		try {
			entity = (MemberEntityImpl) em
					.createNamedQuery("Member.findByEmail")
					.setParameter("email", email).getSingleResult();
			throw new EmailExistenceException("Email(" + email
					+ ") is existence!!");
		} catch (NoResultException e) {
			// could regist.
			try {
				entity = (MemberEntityImpl) em
						.createNamedQuery("Member.findByEmailAndName")
						.setParameter("email", email)
						.setParameter("name", name).getSingleResult();
				throw new NameExistenceException(
						"Email could regist,but name is Existence!!Suggesting renamed!!");
			} catch (NoResultException e1) {
				entity = new MemberEntityImpl(email, password, name);
				em.persist(entity);
				return entity;
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.cit.outman.member.api.persistence.MemberPersistenceService#saveMember
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public IMemberEntity saveMember(String email, String password)
			throws EmailExistenceException {
		MemberEntityImpl entity = null;
		try {
			entity = (MemberEntityImpl) em
					.createNamedQuery("Member.findByEmail")
					.setParameter("email", email).getSingleResult();
			throw new EmailExistenceException("Email(" + email
					+ ") is existence!!");
		} catch (NoResultException e) {
			// could regist.
			entity = new MemberEntityImpl(email, password);
			em.persist(entity);
			return entity;
		}
	}
}
